#import "SongView.h"


@implementation SongView

-(id)initWithFrame:(CGRect)frame
{
	self = [super initWithFrame:frame];
	app = [GuitarSongsApplication sharedInstance];

	float white[] = {1, 1, 1, 1};
	float alpha[] = {1, 1, 1, 0.5};

    struct CGRect rect = [UIHardware fullScreenApplicationContentRect];
    rect.origin.x = rect.origin.y = 0.0f;

	mainView = [[UIView alloc] initWithFrame: rect];
	
	smallFont = [NSClassFromString(@"WebFontCache") createFontWithFamily:@"Helvetica" traits:2 size:9];
	mediumFont = [NSClassFromString(@"WebFontCache") createFontWithFamily:@"Helvetica" traits:2 size:12];
	bigFont = [NSClassFromString(@"WebFontCache") createFontWithFamily:@"Helvetica" traits:2 size:16];

	btnImageNext = [UIImage imageAtPath:[[NSBundle mainBundle] pathForResource:@"next" ofType:@"png" inDirectory:@"/images"]];
	btnImageNextPressed = [UIImage imageAtPath:[[NSBundle mainBundle] pathForResource:@"nextPressed" ofType:@"png" inDirectory:@"/images"]];

	UIImageView *background = [[[UIImageView alloc] initWithFrame:CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height)] autorelease];

	// [background setImage:[[UIImage imageAtPath:[[NSBundle mainBundle] pathForResource:@"emptyBackground" ofType:@"png" inDirectory:@"/images"]] retain]];
	[background setImage:[UIImage defaultDesktopImage]];
	[mainView addSubview:background];
	
	scroll = [[UIScroller alloc] initWithFrame: CGRectMake(0,10.0,320,177)];
	[scroll setScrollingEnabled:YES];
	[scroll setAllowsFourWayRubberBanding:NO];
	[scroll setRubberBand: 20.f forEdges: 5];
	[scroll setShowScrollerIndicators:NO];
	[scroll setScrollDecelerationFactor:0.996f];	
	UIImage *tempImg = [[UIImage imageAtPath:[[NSBundle mainBundle] pathForResource:@"largeFretboard" ofType:@"png" inDirectory:@"/images"]] retain];
	[scroll setContentSize:[tempImg size]];	
	imgBackground = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, [tempImg size].width, [tempImg size].height)] autorelease];
	[imgBackground setImage:tempImg];
	[scroll addSubview:imgBackground];
	// [mainView addSubview:scroll];

	// songText = [[UITextView alloc] initWithFrame: CGRectMake(0,200.0,320,200)];
	songText = [[UITextView alloc] initWithFrame:rect];
	[songText setBackgroundColor: CGColorCreate(CGColorSpaceCreateDeviceRGB(), alpha)];
	[songText setTextSize:14];
	[songText setEditable:NO];
	[songText setHTML:[self readFile:[NSString stringWithFormat:@"%@/resources/A te.htm",[[NSBundle mainBundle] bundlePath]]]];
	// [songText setText:[self readFile:[NSString stringWithFormat:@"%@/resources/A te.txt",[[NSBundle mainBundle] bundlePath]]]];
	[mainView addSubview:songText];

	next = [[UIPushButton alloc] initWithFrame:CGRectMake(10.0, 410.0, 30.0, 37.0)];
	[next setAutosizesToFit:NO];
	[next setDrawsShadow: NO];
	[next setStretchBackground:NO];
	[next setBackground:btnImageNext forState:0];
	[next setBackground:btnImageNextPressed forState:1];
	[next setDrawContentsCentered:YES];
	[next addTarget:self action:@selector(nextButtonPressed:) forEvents:kUIControlEventMouseUpInside];
	[mainView addSubview:next];
	
	[self addSubview:mainView];
			
	return self;
}

- (void)nextButtonPressed:(UIPushButton *)button
{
	NSLog(@"nextButtonPressed");
	// struct CGSize textViewSize=[songText contentSize];
	// NSLog(@"songText width:%f height:%f",textViewSize.width,textViewSize.height);
	[self doScrollAnimation];
}

- (void)doScrollAnimation
{
	[songText setScrollToPointAnimationDuration:20];
	[songText scrollByDelta:CGSizeMake(0,[songText contentSize].height) animated:YES];
}

- (NSString*)readFile:(NSString *)fileName
{
	NSLog(@"%@",fileName);

	char testo[4096*4];
	char line[100*4];
	int lcount=0;
	FILE *infile;

	testo[0]='\0';
	if ((infile=fopen([fileName lossyCString],"r")) == NULL)
	{
	    printf("Error Opening File.\n");
	    exit(1);
	}
	
	while( fgets(line, sizeof(line), infile) != NULL ) {
	    lcount++;
		if (strlen(testo)>0) sprintf(testo,"%s%s",testo,line);
		else sprintf(testo,"%s",line);
	}		
	fclose(infile);
	
	return [NSString stringWithFormat:@"%s",testo];
}

@end